# -*- encoding: UTF-8 -*-
import argparse
from naoqi import ALProxy
import time
import wave
tts = audio = record = aup = None 
record_path = '/home/nao/record.wav'
class TuringChatMode(object): #图灵机器人
    #初始化API请求地址
    def __init__(self):
        # API接口地址
        self.turing_url = 'http://www.tuling123.com/openapi/api?'

     #定义人机交互方法
    def botInteraction (self,text):
      
        url_data = dict(
            key = 'e7ea86036040426e8a9d123176bfe12f',
            info = text,
            userid = 'yjc',
        )
      
        self.request = Request(self.turing_url + urlencode(url_data))#设置并实例化Request

        try:
            w_data = urlopen(self.request)#发送请求
        except URLError:
            raise Exception("No internet connection available to transfer txt data")
            #断言了请求URL异常
        except:
            raise KeyError("Server wouldn't respond (invalid key or quota has been maxed out)")
            # 其他情况断言提示服务相应次数已经达到上限

        response_text = w_data.read().decode('utf-8')
        #print(response_text)

        json_result = json.loads(response_text)#将json格式进行解析

        return json_result['text']

def main(robot_IP, robot_PORT=9559): #录音
    global tts, audio, record, aup 
    tts = ALProxy("ALTextToSpeech", robot_IP, robot_PORT)
    audio = ALProxy("ALAudioDevice", robot_IP, robot_PORT)
    record = ALProxy("ALAudioRecorder", robot_IP, robot_PORT)
    aup = ALProxy("ALAudioPlayer", robot_IP, robot_PORT)
    print 'start recording...'
    record.startMicrophonesRecording(record_path, 'wav', 16000, (0,0,1,0))
    time.sleep(5)
    record.stopMicrophonesRecording()
    print 'record over'
    fileID = aup.playFile(record_path, 0.7, 0)  
    

    
def listen(): #百度
  with open(record_path, 'rb') as fp:
    voices = fp.read()
  try:
   
    result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, })
    result_text = result["result"][0]
    result_text = result_text.replace('，','')
    result_text = result_text.replace('。','')
    print("you said: " + result_text)
    return result_text
  except KeyError:
    print("faild")
    
if __name__ == "__main__":  #主函数
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="169.254.156.103", help="Robot ip address")
    parser.add_argument("--port", type=int, default=9559, help="Robot port number")
    args = parser.parse_args()
    turing = TuringChatMode()
    robot_IP='169.254.156.103'
    robot_PORT=9559
    tts = ALProxy("ALTextToSpeech", robot_IP, robot_PORT)
    while True:
        main(args.ip, args.port)
        msg = listen()
        botMsg = turing.botInteraction(msg)
        bot = str (botMsg)
        tts.say(bot)